package com.shasselclub.project.ask.zhuliRecord.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import com.shasselclub.common.utils.DateUtils;
import com.shasselclub.common.utils.map.GetParamToMap;
import com.shasselclub.common.utils.string.StringUtil;
import com.shasselclub.project.ask.question.domain.Question;
import com.shasselclub.project.ask.question.service.IQuestionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shasselclub.framework.aspectj.lang.annotation.Log;
import com.shasselclub.framework.aspectj.lang.enums.BusinessType;
import com.shasselclub.project.ask.zhuliRecord.domain.ZhuliRecord;
import com.shasselclub.project.ask.zhuliRecord.service.IZhuliRecordService;
import com.shasselclub.framework.web.controller.BaseController;
import com.shasselclub.framework.web.domain.AjaxResult;
import com.shasselclub.common.utils.poi.ExcelUtil;
import com.shasselclub.framework.web.page.TableDataInfo;

/**
 * 好友助力记录Controller
 * 
 * @author zhengyunfei
 * @date 2022-08-08
 */
@Controller
@RequestMapping("/ask/zhuliRecord")
public class ZhuliRecordController extends BaseController
{
    private String prefix = "ask/zhuliRecord";

    @Autowired
    private IZhuliRecordService zhuliRecordService;
    @Autowired
    private IQuestionService questionService;
    @RequiresPermissions("ask:zhuliRecord:view")
    @GetMapping()
    public String zhuliRecord()
    {
        return prefix + "/zhuliRecord";
    }

    /**
     * 查询好友助力记录列表
     */
    @RequiresPermissions("ask:zhuliRecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZhuliRecord zhuliRecord)
    {
        //查询问卷信息
        String questionId=zhuliRecord.getQuestionId();
        if(StringUtil.isNullOrEmpty(questionId)){
            Question question= questionService.selectQuestion();
            questionId=getHdId(question);
        }
        Map<String,Object> queryMap= GetParamToMap.transBean2Map(zhuliRecord);
        queryMap.put("questionId", questionId);
        startPage();
        List<ZhuliRecord> list = zhuliRecordService.selectZhuliRecordList(queryMap);
        return getDataTable(list);
    }
    /**
     * 获取活动问卷id
     * @param question
     * @return
     */
    private String getHdId(Question question) {
        Date startTime= question.getStartTime();
        Date endTime= question.getEndTime();
        //获取本次活动id
        return DateUtil.format(startTime, DateUtils.YYYYMMDDHHMMSS)+DateUtil.format(endTime,DateUtils.YYYYMMDDHHMMSS);
    }
    /**
     * 导出好友助力记录列表
     */
    @RequiresPermissions("ask:zhuliRecord:export")
    @Log(title = "好友助力记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZhuliRecord zhuliRecord)
    {
        Map<String,Object> queryMap= GetParamToMap.transBean2Map(zhuliRecord);
        List<ZhuliRecord> list = zhuliRecordService.selectZhuliRecordList(queryMap);
        ExcelUtil<ZhuliRecord> util = new ExcelUtil<ZhuliRecord>(ZhuliRecord.class);
        return util.exportExcel(list, "好友助力记录数据");
    }

    /**
     * 新增好友助力记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存好友助力记录
     */
    @RequiresPermissions("ask:zhuliRecord:add")
    @Log(title = "好友助力记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZhuliRecord zhuliRecord)
    {
        return toAjax(zhuliRecordService.insertZhuliRecord(zhuliRecord));
    }

    /**
     * 修改好友助力记录
     */
    @RequiresPermissions("ask:zhuliRecord:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ZhuliRecord zhuliRecord = zhuliRecordService.selectZhuliRecordById(id);
        mmap.put("zhuliRecord", zhuliRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存好友助力记录
     */
    @RequiresPermissions("ask:zhuliRecord:edit")
    @Log(title = "好友助力记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZhuliRecord zhuliRecord)
    {
        return toAjax(zhuliRecordService.updateZhuliRecord(zhuliRecord));
    }

    /**
     * 删除好友助力记录
     */
    @RequiresPermissions("ask:zhuliRecord:remove")
    @Log(title = "好友助力记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zhuliRecordService.deleteZhuliRecordByIds(ids));
    }
}
